using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using ODT.Common.Core;
using ODT.Common.Services;
using PPChartControl.Properties;

namespace PPChartControl
{
	public class MovingAverageFilter : Form
	{
		public bool mafiltered;

		public int orderoffilteredsignals;

		public bool filtersignals;

		public int filterorder;

		private string dataInputText;

		private string invalidFormatText;

		private IContainer components;

		private CheckBox MAFiltercheckBox;

		private Button mafilterOkbutton;

		private NumericUpDown mafilterordernumericUpDown;

		public MovingAverageFilter()
		{
			InitializeComponent();
			base.Icon = Resources.ApplicationIcon;
			base.ShowIcon = true;
			SetLanguageTranslations();
		}

		public MovingAverageFilter(bool isfiltered, int filteredorder)
		{
			mafiltered = isfiltered;
			orderoffilteredsignals = filteredorder;
			InitializeComponent();
			base.Icon = Resources.ApplicationIcon;
			base.ShowIcon = true;
			SetLanguageTranslations();
		}

		private void MA_filterForm_Load(object sender, EventArgs e)
		{
			if (mafiltered)
			{
				MAFiltercheckBox.Checked = true;
				mafilterordernumericUpDown.Value = Convert.ToDecimal(orderoffilteredsignals);
			}
			else
			{
				MAFiltercheckBox.Checked = false;
				mafilterordernumericUpDown.Enabled = false;
			}
		}

		private void mafilterOkbutton_Click(object sender, EventArgs e)
		{
			if (MAFiltercheckBox.Checked)
			{
				filtersignals = true;
			}
			else
			{
				filtersignals = false;
			}
			bool bFlag = false;
			string prvVal = mafilterordernumericUpDown.Text;
			mafilterordernumericUpDown.Text = ValidateUnsignedInt(mafilterordernumericUpDown.Text, prvVal, out bFlag);
			if (!bFlag)
			{
				mafilterordernumericUpDown.Focus();
			}
			else
			{
				filterorder = Convert.ToInt32(mafilterordernumericUpDown.Value);
			}
		}

		private string ValidateUnsignedInt(string number, string prvVal, out bool bFlag)
		{
			uint num = 0u;
			try
			{
				num = uint.Parse(number);
				bFlag = true;
				return num.ToString();
			}
			catch (FormatException)
			{
				MessageBox.Show(this, invalidFormatText, dataInputText, MessageBoxButtons.OK, MessageBoxIcon.Hand);
				bFlag = false;
				return prvVal;
			}
			catch (ArgumentNullException)
			{
				MessageBox.Show(this, invalidFormatText, dataInputText, MessageBoxButtons.OK, MessageBoxIcon.Hand);
				bFlag = false;
				return prvVal;
			}
			catch (ArgumentException)
			{
				MessageBox.Show(this, invalidFormatText, dataInputText, MessageBoxButtons.OK, MessageBoxIcon.Hand);
				bFlag = false;
				return prvVal;
			}
			catch (OverflowException)
			{
				MessageBox.Show(this, invalidFormatText, dataInputText, MessageBoxButtons.OK, MessageBoxIcon.Hand);
				bFlag = false;
				return prvVal;
			}
		}

		private void MAFiltercheckBox_CheckedChanged(object sender, EventArgs e)
		{
			if (MAFiltercheckBox.Checked)
			{
				mafilterordernumericUpDown.Enabled = true;
			}
			else
			{
				mafilterordernumericUpDown.Enabled = false;
			}
		}

		private void SetLanguageTranslations()
		{
			ILanguageTranslationService service = ServiceManager.GetService<ILanguageTranslationService>();
			dataInputText = service.Translate("PPChartControl.DataInput");
			invalidFormatText = service.Translate("PPChartControl.InvalidFormat");
			SuspendLayout();
			MAFiltercheckBox.Text = service.Translate("PPChartControl.FilterSignalsUsingAMovingAverageFilterOfOrder");
			mafilterOkbutton.Text = service.Translate("Common.Ok");
			Text = service.Translate("PPChartControl.MovingAverageFilter");
			ResumeLayout(false);
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing && components != null)
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		private void InitializeComponent()
		{
			this.MAFiltercheckBox = new System.Windows.Forms.CheckBox();
			this.mafilterOkbutton = new System.Windows.Forms.Button();
			this.mafilterordernumericUpDown = new System.Windows.Forms.NumericUpDown();
			((System.ComponentModel.ISupportInitialize)this.mafilterordernumericUpDown).BeginInit();
			base.SuspendLayout();
			this.MAFiltercheckBox.AutoSize = true;
			this.MAFiltercheckBox.Location = new System.Drawing.Point(12, 30);
			this.MAFiltercheckBox.Name = "MAFiltercheckBox";
			this.MAFiltercheckBox.Size = new System.Drawing.Size(157, 30);
			this.MAFiltercheckBox.TabIndex = 0;
			this.MAFiltercheckBox.Text = "Filter signals using a moving\r\naverage filter of order  ";
			this.MAFiltercheckBox.UseVisualStyleBackColor = true;
			this.MAFiltercheckBox.CheckedChanged += new System.EventHandler(MAFiltercheckBox_CheckedChanged);
			this.mafilterOkbutton.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.mafilterOkbutton.Location = new System.Drawing.Point(71, 82);
			this.mafilterOkbutton.Name = "mafilterOkbutton";
			this.mafilterOkbutton.Size = new System.Drawing.Size(98, 30);
			this.mafilterOkbutton.TabIndex = 1;
			this.mafilterOkbutton.Text = "OK";
			this.mafilterOkbutton.UseVisualStyleBackColor = true;
			this.mafilterOkbutton.Click += new System.EventHandler(mafilterOkbutton_Click);
			this.mafilterordernumericUpDown.Location = new System.Drawing.Point(185, 36);
			this.mafilterordernumericUpDown.Maximum = new decimal(new int[4] { 10, 0, 0, 0 });
			this.mafilterordernumericUpDown.Name = "mafilterordernumericUpDown";
			this.mafilterordernumericUpDown.Size = new System.Drawing.Size(46, 20);
			this.mafilterordernumericUpDown.TabIndex = 2;
			this.mafilterordernumericUpDown.Value = new decimal(new int[4] { 2, 0, 0, 0 });
			base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 13f);
			base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			base.ClientSize = new System.Drawing.Size(251, 127);
			base.Controls.Add(this.mafilterordernumericUpDown);
			base.Controls.Add(this.mafilterOkbutton);
			base.Controls.Add(this.MAFiltercheckBox);
			base.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			base.MaximizeBox = false;
			base.MinimizeBox = false;
			base.Name = "MovingAverageFilter";
			base.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "Moving Average Filter";
			base.Load += new System.EventHandler(MA_filterForm_Load);
			((System.ComponentModel.ISupportInitialize)this.mafilterordernumericUpDown).EndInit();
			base.ResumeLayout(false);
			base.PerformLayout();
		}
	}
}
